/*
 * @Author: chunlai
 * @Date: 2023-07-25 09:48:14
 * @LastEditTime: 2025-08-27 14:50:21
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \02.bldinsure\src\store\modules\varietyInfo.js
 * @Description: 险种接口信息
 */
import consts from "@/config/const.js";
import { safeParseJSON } from "@/methods/format.js";
// import riskConfig from "@/config/risk.json";

const state = {
  isOnline: "0", // 1:互联网 2:线下 3:MGA
  inAppFlag: "", //是否APP对接0是 1不是
  abutmentType: "0", //对接方式1-H5  0-API
  describeLabel: "", //小标签
  describeText: "", //分享描述
  contentUrl: "", //分享图标
  gysName: "", //供应商名称
  varietyName: "", //险种名称
  productName: "", //产品名称
  productCode: "", //产品编码
  insureStatus: "", //产品状态 0在售 1停售
  lowestPrice: "", //最低保费
  shareUrl: "",
  json1: {
    posters: [],
  }, //页面配置数据 index.json
  dutys: [],
  json2: {}, //险种及校验配置 risk.json
  json3: {}, //必填字段及年龄配置 infoReg.json
  json4: {}, //页面项配置 page.json
  // json5: {}, //健康告知healthy.json
  // json6: {}, //重庆告知 chognqing.json
  provinces: [],
  riskConfig: -1,
  continueUrl: "",
};
const getters = {
  describeLabel: (state) => state.describeLabel,
  describeText: (state) => state.describeText,
  gysName: (state) => state.gysName,
  insureStatus: (state) => state.insureStatus,
  contentUrl: (state) => state.contentUrl,
  productName: (state) => state.productName,
  isOnline: (state) => state.isOnline,
  inAppFlag: (state) => state.inAppFlag,
  lowestPrice: (state) => state.lowestPrice,
  abutmentType: (state) => state.abutmentType,
  shareUrl: (state) => state.shareUrl,
  provinces: (state) => state.provinces,
  dutys: (state) => state.dutys,
  productCode: (state) => state.productCode,
  posters: (state) => state.json1.posters,
};
const mutations = {
  INIT_DATA(state, message) {
    Object.assign(state, message);
  },
  SET_PROVINCE(state, list) {
    state.provinces = list;
  },
  SET_LOWESR_PRICE(state, list) {
    state.lowestPrice = list;
  },
  SET_RISK_CONFIG(state, data) {
    state.riskConfig = data;
  },
  SET_CONTINUE_URL(state, data) {
    state.continueUrl = data;
  },
};
const actions = {
  initData({ commit, dispatch, state, rootGetters }, data) {
    let { varietyInfo, relateVarietys, productNotices, gysBranches } = data;
    /**销售区域 */
    if (gysBranches && gysBranches.length > 0) {
      commit("SET_PROVINCE", gysBranches);
    }
    /**首页必读弹出框 */
    if (productNotices?.length > 0) {
      commit(
        "page/SET_PAGE_CONFIG",
        {
          productNoticesList: productNotices,
        },
        {
          root: true,
        }
      );
    }
    /**产品信息 */
    // 主险
    if (varietyInfo?.msProduct) {
      if (varietyInfo?.msProduct?.productNo) {
        commit("insert/SET_UNIQUE_ID", varietyInfo.msProduct.productNo, {
          root: true,
        });
      }
      commit("INIT_DATA", varietyInfo.msProduct);
    }

    let target = varietyInfo?.productDetail || {};
    let obj = {
      json1: safeParseJSON(target.json1) || {},
      json2: safeParseJSON(target.json2) || {},
      hsybRiskInfo: safeParseJSON(target.hsybRiskInfo) || {}, //比来多险种配置
      json3: safeParseJSON(target.json3) || {},
      json4: safeParseJSON(target.json4) || {},
      readFileList: safeParseJSON(target.readFileList) || [],
    };
    commit(
      "home/updateField",
      {
        shareInfo: {
          title: varietyInfo.msProduct.productName,
          desc: varietyInfo.msProduct.describeText,
          icon: varietyInfo.msProduct.contentUrl,
        },
      },
      {
        root: true,
      }
    );
    if (!consts.empty(obj.json1)) {
      commit("home/updateField", obj.json1, {
        root: true,
      });
    }
    commit("INIT_DATA", obj);
    /**
     * productReadFiles: 产品解读类 pdf条款
     */
    // console.log(obj.json1)
    let fileArr = [];
    if (varietyInfo.msProduct.productReadFiles.length > 0) {
      fileArr = varietyInfo.msProduct.productReadFiles.filter(
        (ele) => ele.insuranceUse == "1"
      );
    }
    // 产品名称由msProduct.productName确定
    const bldRiskJson = obj.hsybRiskInfo || {};
    if (bldRiskJson.continueUrl) {
      commit("SET_CONTINUE_URL", bldRiskJson.continueUrl);
    }
    let mainRiks = {
      ...obj.json2,
      ...bldRiskJson,
      ismain: true,
      varietyCode: varietyInfo.msProduct.varietyNo,
      fileList: fileArr,
      productName: varietyInfo.msProduct.productName,
    };
    if (mainRiks.productName) {
      document.title = mainRiks.productName;
    }
    if (mainRiks.lowestPrice) {
      commit("SET_LOWESR_PRICE", mainRiks.lowestPrice);
    }
    // 备案名
    if (mainRiks.recordName) {
      commit("home/SET_RECORD_NAME", mainRiks.recordName, {
        root: true,
      });
    }
    // 过滤在售附加险
    let onlineSub = relateVarietys.filter(
      (item) => item.msProduct && item.msProduct.insureStatus == "0"
    );

    let subRisks = onlineSub.map((item) => {
      const readpdfs = item.msProduct.productReadFiles || [];
      let file2Arr = [];
      if (readpdfs.length > 0) {
        file2Arr = readpdfs.filter((ele) => ele.insuranceUse == "1");
      }
      let targets = {
        json2: safeParseJSON(item.productDetail.json2) || {},
        readFileList: file2Arr,
      };
      return {
        ...targets.json2,
        fileList: targets.readFileList,
        varietyCode: item.msProduct.varietyNo,
        productName: item.msProduct.productName,
      };
    });
    // 投保前置配置
    if (mainRiks.preInsConfig !== void 0) {
      commit("home/SET_PRE_INSURE", mainRiks.preInsConfig, {
        root: true,
      });
    }
    let riskList = [];
    // riskList = [...riskConfig.mainRiks, ...riskConfig.subRisks]; // 本地预测试
    riskList = [mainRiks, ...subRisks];
    if (state.riskConfig === -1) {
      commit("SET_RISK_CONFIG", 1);
      commit("risks/SET_RISKSTATE", riskList, {
        root: true,
      });
    }
    commit("insert/SET_RECALL", "1", {
      root: true,
    });
    dispatch("home/setClauseFiles", riskList, {
      root: true,
    });

    /*产品特殊后台配置的直接使用,否则,使用公共的 infoReg page*/
    // page类配置
    if (!consts.empty(obj.json4)) {
      dispatch("page/initPageConfig", obj.json4, {
        root: true,
      });
      if (obj.json4.preInsConfig !== void 0) {
        commit("home/SET_PRE_INSURE", obj.json4.preInsConfig, {
          root: true,
        });
      }
    }
    // console.log('---infoReg', obj.json3);
    // (1).接口返回 非inputReg公共字段
    dispatch("infoReg/initStoreData", obj.json3, {
      root: true,
    });
    // 特殊险种码表配置
    dispatch("infoReg/initDicStore", obj.json3, {
      root: true,
    });
  },
};
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
